package com.duguyuan.service;

import com.duguyuan.utils.DBUtil;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;

public class MyInvocationHandler implements InvocationHandler {

    public UserService userService;

    public MyInvocationHandler(UserService userService) {
        this.userService = userService;
    }

    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        Object ret = null; // 定义返回值
        try {
            // System.out.println("开启事务");
            DBUtil.begin(); // 开启事务
            ret = method.invoke(userService, args);
            DBUtil.commit(); // 事务提交
            // System.out.println("事务提交");
            return ret;
        } catch (Exception e) {
            e.printStackTrace();
            DBUtil.rollback(); // 事务回滚
            // System.out.println("事务回滚");
        } finally {
        }
        return null;
    }
}
